{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {
    "metadata": {}
   },
   "outputs": [],
   "source": [
    "import json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {
    "metadata": {}
   },
   "outputs": [],
   "source": [
    "data = json.loads(open('new_desc.json', encoding='utf-8').read())\n",
    "data = [json.loads(x) for x in data]\n",
    "endata = data[::2]\n",
    "chdata = data[1::2]\n",
    "assert len(endata) == len(chdata)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {
    "metadata": {}
   },
   "outputs": [],
   "source": [
    "allres = {}\n",
    "ename_to_cname = {}\n",
    "version = 'X.X'\n",
    "for ed, cd in zip(endata, chdata):\n",
    "    for [ek, ev], [ck, cv] in zip(ed, cd):\n",
    "        k = ek\n",
    "        if k[:5] == 'SKILL':\n",
    "            ori_k = k\n",
    "            name = k.split('_')[1]\n",
    "            allres['CHARACTER/' + name] = {}\n",
    "            k = k.replace('NORMAL ATTACK', 'NORMAL_ATTACK')\n",
    "            k = k.replace('ELEMENTAL SKILL', 'ELEMENTAL_SKILL')\n",
    "            k = k.replace('ELEMENTAL BURST', 'ELEMENTAL_BURST')\n",
    "            k = k.replace('PASSIVE SKILL', 'PASSIVE')\n",
    "            mat = {\n",
    "                'NORMAL_ATTACK': '普通攻击',\n",
    "                'ELEMENTAL_SKILL': '元素战技',\n",
    "                'ELEMENTAL_BURST': '元素爆发',\n",
    "                'PASSIVE': '被动技能',\n",
    "            }\n",
    "            c_skill_type = None\n",
    "            for etype in mat:\n",
    "                if etype in k:\n",
    "                    c_skill_type = mat[etype]\n",
    "                    break\n",
    "            ename_to_cname[k] = ck\n",
    "            allres[k] = ev\n",
    "            allres[k]['names'].update(cv['names'])\n",
    "            version = list(allres[k]['descs'].keys())[0]\n",
    "            allres[k]['descs'][version].update(cv['descs'][version])\n",
    "        else:\n",
    "            ename_to_cname[k] = ck\n",
    "            allres[k] = ev\n",
    "            allres[k]['names'].update(cv['names'])\n",
    "            version = list(allres[k]['descs'].keys())[0]\n",
    "            allres[k]['descs'][version].update(cv['descs'][version])\n",
    "# print(json.dumps(allres, indent = 2, ensure_ascii = False))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {
    "metadata": {}
   },
   "outputs": [],
   "source": [
    "new_card_data = json.load(open('new_cards.json'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {
    "metadata": {}
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[\"SKILL_Kuki Shinobu_NORMAL_ATTACK/Shinobu's Shadowsword\", 'SKILL_Kuki Shinobu_ELEMENTAL_SKILL/Sanctifying Ring', 'SKILL_Kuki Shinobu_ELEMENTAL_BURST/Gyoei Narukami Kariyama Rite', 'TALENT_Kuki Shinobu/To Ward Weakness']\n",
      "['SKILL_Faruzan_NORMAL_ATTACK/Parthian Shot', 'SKILL_Faruzan_ELEMENTAL_SKILL/Wind Realm of Nasamjnin', \"SKILL_Faruzan_ELEMENTAL_BURST/The Wind's Secret Ways\", 'TALENT_Faruzan/The Wondrous Path of Truth']\n",
      "['SKILL_Abyss Herald: Wicked Torrents_NORMAL_ATTACK/Rippling Slash', 'SKILL_Abyss Herald: Wicked Torrents_ELEMENTAL_SKILL/Vortex Edge', 'SKILL_Abyss Herald: Wicked Torrents_ELEMENTAL_BURST/Torrential Shock', 'SKILL_Abyss Herald: Wicked Torrents_PASSIVE/Watery Rebirth', 'SKILL_Abyss Herald: Wicked Torrents_ELEMENTAL_SKILL/Rippling Blades']\n",
      "['SKILL_Emperor of Fire and Iron_NORMAL_ATTACK/Shatterclamp Strike', 'SKILL_Emperor of Fire and Iron_ELEMENTAL_SKILL/Buster Blaze', 'SKILL_Emperor of Fire and Iron_ELEMENTAL_BURST/Battle-Line Detonation', 'SKILL_Emperor of Fire and Iron_PASSIVE/Imperial Panoply', 'SKILL_Emperor of Fire and Iron_ELEMENTAL_BURST/Searing Blast', 'TALENT_Emperor of Fire and Iron/Molten Mail']\n",
      "['TALENT_Kuki Shinobu/To Ward Weakness']\n",
      "['TALENT_Faruzan/The Wondrous Path of Truth']\n",
      "['TALENT_Abyss Herald/Surging Undercurrent']\n",
      "['TALENT_Emperor of Fire and Iron/Molten Mail']\n",
      "['WEAPON/Rightful Reward']\n",
      "['ARTIFACT/Amethyst Crown']\n",
      "['SUPPORT/Seirai Island']\n",
      "['SUPPORT/Taroumaru']\n",
      "['SUPPORT/The White Glove and the Fisherman']\n",
      "['CARD/Underwater Treasure Hunt']\n",
      "['CARD/Rainbow Macarons']\n"
     ]
    }
   ],
   "source": [
    "for image_path, file_name, name, id, URL in new_card_data:\n",
    "    id = int(id)\n",
    "    keys = list(allres.keys())\n",
    "    selected_keys = [x for x in keys if name in x and 'CHARACTER/' not in x]\n",
    "    if id < 100000:\n",
    "        # is character\n",
    "        cname = ename_to_cname[selected_keys[0]].split('_')[1]\n",
    "        allres['CHARACTER/' + name] = {\n",
    "          'names': {\n",
    "              'en-US': name,\n",
    "              'zh-CN': cname,\n",
    "          },\n",
    "          'descs': {\n",
    "              version: {\n",
    "                  'en-US': '',\n",
    "                  'zh-CN': ''\n",
    "              }\n",
    "          },\n",
    "          'image_path': image_path,\n",
    "          'id': id,\n",
    "        }\n",
    "        continue\n",
    "    if len(selected_keys) != 1:\n",
    "        print('Cannot find', name, 'in', selected_keys)\n",
    "        continue\n",
    "    key = selected_keys[0]\n",
    "    allres[key]['image_path'] = image_path\n",
    "    allres[key]['id'] = id"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {
    "metadata": {}
   },
   "outputs": [],
   "source": [
    "json.dump(allres, open('new_result.json', 'w', encoding='utf-8'), \n",
    "          ensure_ascii=False, indent=4)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "GITCG",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
